package com.erp.warehouse.clouderp_warehouse.service.impl.merge;

import com.erp.warehouse.clouderp_warehouse.mapper.merge.GoodsMergeDetailMapper;
import com.erp.warehouse.clouderp_warehouse.mapper.merge.GoodsMergeMapper;
import com.erp.warehouse.clouderp_warehouse.pojo.merge.GoodsMerge;
import com.erp.warehouse.clouderp_warehouse.pojo.merge.GoodsMergeCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.merge.GoodsMergeDetail;
import com.erp.warehouse.clouderp_warehouse.pojo.merge.GoodsMergeModify;
import com.erp.warehouse.clouderp_warehouse.service.merge.MergeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 2025/11/13 14:09
 * author: 向远洋
 */
@Service
@Transactional
public class MergeServiceImpl implements MergeService {
    @Autowired
    GoodsMergeMapper mapper;
    @Autowired
    GoodsMergeDetailMapper detailMapper;

    @Override
    public PageInfo<GoodsMerge> getPageInfo(Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsMerge> list = mapper.selectGoodsMergeList(params);
        return new PageInfo<>(list);
    }

    @Override
    public void createWithItems(GoodsMergeCreate request) {
        GoodsMerge goodsMerge = request.getGoodsMerge();
        goodsMerge.setId(UUID.randomUUID().toString());
        List<GoodsMergeDetail> detailList = request.getDetailList();
        mapper.insertMerge(goodsMerge);
        for (GoodsMergeDetail goodsMergeDetail : detailList) {
            goodsMergeDetail.setId(UUID.randomUUID().toString());
            goodsMergeDetail.setMergeId(goodsMerge.getId());
        }
        detailMapper.insertGoodsMergeDetailList(detailList);
    }

    @Override
    public GoodsMergeCreate getMergeWithDetail(String id) {
        GoodsMerge goodsMerge = mapper.selectGoodsMerge(id);
        List<GoodsMergeDetail> list = detailMapper.selectListByGoodsMergeId(id);
        return new GoodsMergeCreate(goodsMerge, list);
    }

    @Override
    public void modifyMergeWithDetail(GoodsMergeModify mergeModify) {
        // 执行删除操作
        List<String> delList = mergeModify.getDelList();
        if (!delList.isEmpty()) {
            int count = detailMapper.deleteGoodsMergeDetailList(delList);
            if (count != delList.size()) {
                throw new RuntimeException("删除组装明细失败！");
            }
        }

        // 修改操作
        List<GoodsMergeDetail> modList = mergeModify.getModList();
        if (!modList.isEmpty()) {
            for (GoodsMergeDetail mergeDetail : modList) { //这里需要判断是序列号商品还是普通商品 --------未完成--------
                int count = detailMapper.updateGoodsMergeDetail(mergeDetail);
                if (count != 1) {
                    throw new RuntimeException("修改组装明细失败！");
                }
            }
        }

        // 新增操作
        List<GoodsMergeDetail> addList = mergeModify.getAddList();
        for (GoodsMergeDetail mergeDetail : addList) {
            mergeDetail.setId(UUID.randomUUID().toString());
        }
        if (!addList.isEmpty()) {
            detailMapper.insertGoodsMergeDetailList(addList);
        }

// 判断是否需要修改主单
        boolean modified = mergeModify.getOrderModified();
        if (modified) {
            mapper.updateMerge(mergeModify.getGoodsMerge());
        }
    }

    @Override
    public void auditorMergeWithDetail(GoodsMergeCreate request) {

    }
}
